<template>
  <div class="preview-block-item" :class="{ invalid: fileItem.isFollow === '0' }">
    <img v-if="fileItem.previewType === PREVIEW_TYPES.img" class="img-preview" :src="fileItem.previewPath" :title="fileItem.name" @click="$emit('preview')" />
    <div class="video-view" v-if="fileItem.previewType === PREVIEW_TYPES.video" @click="$emit('preview')">
      <img :src="fileItem.thumbnailPath" :title="fileItem.name" />
      <i class="el-icon-video-play play-icon"></i>
    </div>
    <div class="file-view" :title="fileItem.name" v-else-if="fileItem.previewType === PREVIEW_TYPES.doc" @click="$emit('preview')">
      <i class="el-icon-document"></i>
      <div class="file-name">
        <span v-if="fileItem.duration !== '' && fileItem.duration !== '0'" class="word-count" :class="{ 'count-wraning': fileItem.duration < 100 }">
          （{{ fileItem.duration || 0 }}字）
        </span>
        <span>
          {{ fileItem.name }}
        </span>
      </div>
    </div>
    <div class="icon-view" v-if="showDownload || showDelete">
      <i class="icon-btn el-icon-download" v-if="showDownload" @click="$emit('download')" title="下载"></i>
      <i class="icon-btn el-icon-delete" v-if="showDelete" @click="$emit('delete')" title="删除"></i>
    </div>
    <div class="invalid-tips" v-if="fileItem.isFollow === '0'">
      <el-tooltip :content="'缺失关键词：' + keywordDisplay(fileItem.keyword)" placement="top">
        <i class="el-icon-warning"></i>
      </el-tooltip>
    </div>
  </div>
</template>

<script>
import { FILE_PREVIEW_TYPE } from '@utility/dict'
export default {
  name: 'PreviewBlockItem',
  components: {},
  props: {
    fileItem: {
      type: Object,
    },
    showDownload: {
      type: Boolean,
      default: true,
    },
    showDelete: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      PREVIEW_TYPES: FILE_PREVIEW_TYPE._define,
    }
  },
  methods: {
    keywordDisplay(keyword) {
      if (keyword.indexOf('_') !== -1) {
        let keywordArr = []
        keywordArr = keyword.split('_').filter(li => {
          return li !== ''
        })
        return keywordArr.join('；')
      } else {
        return keyword
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.preview-block-item {
  display: inline-block;
  width: 120px;
  height: 68px;
  background: #fff;
  overflow: hidden;
  cursor: pointer;
  position: relative;
  margin: 5px 0;
  border: 1px solid #dcdfe6;
  &:not(:last-child) {
    margin-right: 10px;
  }

  &.invalid {
    border: 1px dashed #f17c80;
  }
  &:hover {
    background: #f0f5fa;
    .icon-view {
      display: flex;
      background: rgba(0, 0, 0, 0.15);
      display: flex;
      flex-direction: row;
      justify-content: flex-end;
      align-items: center;
    }
  }
  .invalid-tips {
    position: absolute;
    top: 0px;
    right: 4px;
    height: 15px;
    width: 15px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 10px;
    color: #d23b41;
    background: #fff;
  }
  .icon-view {
    position: absolute;
    width: 100%;
    bottom: 0;
    left: 0;
    height: 20px;
    display: none;
    .icon-btn {
      margin-left: 5px;
      cursor: pointer;
      color: #fff;
      &:hover {
        // color: #2C63B0;
        font-weight: bold;
      }
      &::before {
        margin-right: 3px;
      }
    }
  }
  img {
    object-fit: cover;
    object-position: center;
    width: 100%;
    height: 100%;
  }
  .video-view {
    width: 100%;
    height: 100%;
    position: relative;
    img::after {
      content: '点击播放';
    }
    &:hover {
      .play-icon {
        color: cornflowerblue;
      }
    }

    .play-icon {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      font-size: 30px;
      color: #ddd;
    }
  }

  .file-view {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
    // border: 1px solid #dcdfe6;
    .el-icon-document {
      font-size: 28px;
      color: cornflowerblue;
      margin: 5px 0;
    }
    .file-name {
      flex: 1;
      width: 100%;
      padding: 0 5px;
      font-size: 14px;
      text-align: center;
      word-break: keep-all;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .word-count {
      font-size: 12px;
    }
    .count-wraning {
      color: #e6a23c;
    }
  }
}
</style>
